<?php
/**
 * This file is form http://findcat.cn
 *
 * @link     https://findcat.cn
 * @email    1476982312@qq.com
 */
namespace Database\Seeder;

use Encore\Admin\Auth\Database\Role;
use Encore\Admin\Auth\Database\Administrator;

/**
 * 生成管理端用户的seeder基础方法.
 *
 * Trait MenuSeederMaker
 */
trait AdminUserSeederMaker
{
    /**
     * 创建管理端用户.
     *
     * @param $username
     * @param $password
     * @param $name
     * @param $roleSlug
     *
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model
     */
    protected function createAdminUsers($username, $password, $name, $roleSlug)
    {
        $adminUser = Administrator::query()->updateOrCreate(
            [
                'username'   => $username,
                'password'   => bcrypt($password),
                'name'       => $name,
            ]
        );

        $role = Role::query()->where('slug', $roleSlug)->first();

        $adminUser->roles()->save($role);

        return $adminUser;
    }
}
